from flask import Blueprint, request, jsonify
import json

from Config.Config import get_redis_client
from blueprints.DataDispose import DataClean

useDataDispose = Blueprint('useDataDispose', __name__)


@useDataDispose.route('/DataDispose', methods=['POST'])
def useDataDispose_post():
    # 处理接收到的数据
    global typeMethod, strategy, n_neighbors
    data = request.form.get("data_url")
    dataName = request.form.get("data_name")
    # 接收到如何进行数据清理
    dataDispose = request.form.get("dispose_type")
    nNeighbors = request.form.get("n_neighbors")
    if dataDispose == 1:
        typeMethod = "mean"
        strategy = None
        n_neighbors = None
    elif dataDispose == 2:
        typeMethod = "median"
        strategy = None
        n_neighbors = None
    elif dataDispose == 3:
        typeMethod = "mode"
        strategy = None
        n_neighbors = None
    elif dataDispose == 4:
        strategy = "most_frequent"
        typeMethod = None
        n_neighbors = None
    elif dataDispose == 5:
        n_neighbors = nNeighbors
        strategy = None
        typeMethod = None

        # 将参数存储到Redis中
    key = "retrieve_params_from_redis_Dispose"
    params = {
        'data_url': data,
        'data_name': dataName,
        'typeMethod': typeMethod,
        'strategy': strategy,
        'n_neighbors': n_neighbors
    }
    redis_client = get_redis_client()
    redis_client.set(key, json.dumps(params))

    return "参数已传递"

